<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jQuery.min3.6.0.js"></script>
</head>

<body>
    <form class="form-container" enctype="multipart/form-data" id='f1'>
        <div class="form-group">
            <label>标题</label>
            <input name="title" type="text" class="form-control" placeholder="请输入文章标题">
        </div>
        <div class="form-group">
            <label>分类id</label>
            <input name="cate_id" type="text" class="form-control" readonly>
        </div>
        <div class="form-group">
            <label>内容</label>
            <input name="content" type="text" class="form-control" readonly>
        </div>

        <div class="form-group">
            <label for="exampleInputFile">文章封面</label>
            <input type="file" name="cover_img" id="file">
            <div class="thumbnail-waper">
                <img class="img-thumbnail" src="" id="preview">
            </div>
        </div>
        <div class="form-group">
            <label>状态</label>
            <input name="state" type="text" class="form-control" readonly>
        </div>
        <div class="buttons">
            <input type="submit" class="btn btn-primary">
        </div>
    </form>
    <script>

        $(function () {
            $('#f1').submit(function (e) {
                e.preventDefault();
                var data = $(this).serialize();
                console.log(data);
                $.ajax({
                    type: "POST",
                    url: "http://127.0.0.1:8080/my/article/add",
                    data: 'data',
                    headers: { 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTEsInVzZXJuYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6IiIsIm5pY2tuYW1lIjoiTGVlIiwiZW1haWwiOiI2OTQ2NDYyMzZAcXEuY29tIiwidXNlcl9waWMiOiIiLCJpYXQiOjE2MjMxMTc3MTIsImV4cCI6MTYyNjcxNzcxMn0.xIASwygbUI4aJ3pTdNK2Pvgh2FVRyp6bK1dvZxrtHQs' },
                    success: function (res) {
                        console.log(res)
                    }
                });
            })

            // 选择文件上传控件
            var file = document.querySelector('#file');
            var preview = document.querySelector('#preview');
            // 当用户选择完文件以后
            file.onchange = function () {
                // 1 创建文件读取对象
                var reader = new FileReader();
                // 用户选择的文件列表
                // console.log(this.files[0])
                // 2 读取文件
                reader.readAsDataURL(this.files[0]);
                // 3 监听onload事件
                reader.onload = function () {
                    console.log(reader.result)
                    // 将文件读取的结果显示在页面中
                    preview.src = reader.result;
                }
            }
        });
    </script>
</body>

</html>